import 'package:flutter/material.dart';
// 假设这里的 http 工具类导入没问题
import 'package:sl_flutter/utils/http.dart';

class DeliveryStatusPage extends StatefulWidget {
  @override
  _DeliveryStatusPageState createState() => _DeliveryStatusPageState();
}

class _DeliveryStatusPageState extends State<DeliveryStatusPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  List<Map<String, dynamic>> taskList = [
    {
      "id": "4051314380064802050",
      "startAgencyId": "1024984540818568481",
      "startAddress": "辽宁抚顺顺城鸭绿江北街58-8号",
      "endAgencyId": "1024993422324415489",
      "endAddress": "湖北武汉武昌友谊大道194号附近",
      "status": 1,
      "driverId": "4051314380044164713",
      "transportTaskId": "4051314380064642046",
      "startHandover": null,
      "finishHandover": null,
      "planDepartureTime": "2024-05-13 20:00:00",
      "actualDepartureTime": null,
      "planArrivalTime": "2024-05-14 06:00:00",
      "actualArrivalTime": null,
      "created": "2024-05-13 14:38:00",
      "enablePickUp": true
    },
  ];

  // 获取任务列表
  void getTaskList() async {
    try {
      var res = await https.get('/driver/tasks/list',
          params: {'page': 1, 'pageSize': 10, 'status': 1});

      setState(() {
        taskList = List<Map<String, dynamic>>.from(res['data']['items']);
      });
      print(taskList);
    } catch (e) {
      print('获取任务列表失败: $e');
    }
  }

  @override
  void initState() {
    super.initState();
    getTaskList();
    _tabController = TabController(length: 3, vsync: this);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        title: TabBar(
          controller: _tabController,
          tabs: [Tab(text: '待提货'), Tab(text: '在途'), Tab(text: '已完成')],
          indicatorColor: Colors.red,
          labelColor: Colors.black,
          unselectedLabelColor: Colors.grey,
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: [
          // 待提货页面
          ListView.builder(
            itemCount: taskList.length,
            itemBuilder: (context, index) {
              Map<String, dynamic> task = taskList[index];
              return Container(
                width: MediaQuery.of(context).size.width * 0.8,
                child: card(
                  '任务编号：${task['id']}',
                  task['startAddress'],
                  task['endAddress'],
                  task['planArrivalTime'],
                  '已延迟', // 这里假设状态固定，可根据实际情况修改
                  // '起',
                  // '止',
                  '提货',
                  '提货时间',
                ),
              );
            },
          ),
          // 在途页面
          Center(
            child: Text('在途页面内容'),
          ),
          // 已完成页面
          Center(
            child: Text('已完成页面内容'),
          ),
        ],
      ),
    );
  }
}

Widget card(
    String taskNumber,
    String startAddress,
    String endAddress,
    String pickUpTime,
    String delayStatus,
    // String startLabel,
    // String endLabel,
    String pickUpButtonText,
    String pickUpTimeLabel) {
  return Padding(
    padding: EdgeInsets.all(20),
    child: Container(
      padding: const EdgeInsets.all(15),
      decoration: BoxDecoration(color: Colors.white),
      height: 230,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                taskNumber,
                style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
              ),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: const Color.fromARGB(255, 255, 255, 255),
                  borderRadius: BorderRadius.circular(30),
                  // 添加边框线
                  border: Border.all(
                    color: Colors.redAccent, // 边框线的颜色
                    width: 1.5, // 边框线的宽度
                  ),
                ),
                child: Text(
                  delayStatus,
                  style: TextStyle(color: Colors.redAccent),
                ),
              ),
            ],
          ),
          SizedBox(height: 16),
          Row(
            children: [
              Center(
                child: CircleAvatar(
                    backgroundColor: const Color.fromARGB(255, 0, 0, 0),
                    radius: 12,
                    child: Column(
                      children: [
                        Text(
                          '起',
                          style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                              color: Colors.white),
                        ),
                      ],
                    )),
              ),
              SizedBox(width: 8),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // Text(
                    //   startLabel,
                    //   style:
                    //       TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    // ),
                    // 设置SoftWrap为true让文本自动换行
                    Text(
                      startAddress,
                      maxLines: 3,
                      style: TextStyle(
                        fontSize: 14,
                      ),
                      softWrap: true,
                    ),
                  ],
                ),
              ),
            ],
          ),
          SizedBox(height: 16),
          Row(
            children: [
              Center(
                child: CircleAvatar(
                    backgroundColor: Colors.redAccent,
                    radius: 12,
                    child: Column(
                      children: [
                        Text(
                          '止',
                          style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                              color: Colors.white),
                        ),
                      ],
                    )),
              ),
              SizedBox(width: 8),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // Text(
                  //   endLabel,
                  //   style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                  // ),
                  // 设置SoftWrap为true让文本自动换行
                  Text(
                    endAddress,
                    style: TextStyle(fontSize: 14),
                    softWrap: true,
                  ),
                ],
              ),
            ],
          ),
          SizedBox(height: 16),
          Row(
            children: [
              Text(
                pickUpTimeLabel,
                style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
              ),
            ],
          ),
          SizedBox(height: 8),
          Row(
            children: [
              Text(
                pickUpTime,
                style: TextStyle(fontSize: 12),
              ),
              SizedBox(width: 80),
              Align(
                alignment: Alignment.bottomRight,
                child: ElevatedButton(
                  onPressed: () {
                    // 提货按钮点击逻辑
                  },
                  child: Text(pickUpButtonText),
                  style: ElevatedButton.styleFrom(
                    foregroundColor: Colors.white,
                    backgroundColor: Colors.redAccent,
                    padding: EdgeInsets.symmetric(horizontal: 32, vertical: 12),
                    textStyle: TextStyle(fontSize: 14, color: Colors.white),
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    ),
  );
}
